﻿class Viewer{
	//------------------------------------  属性 
	[Inspectable(defaultValue="true")]
	public var showHelp:Boolean
	[Inspectable(defaultValue="0")]
	public var findRStopAndBottom:Number
	[Inspectable(defaultValue="0")]
	public var findRSleftAndRight:Number
	//------------------------------------  变量
	//  控制器
	private var myX:Number=0;
	private var myY:Number=0;
	private var myScale:Number=100;
	private var myVisible:Boolean=true;
	private var myTx:Number=4; //缓冲级数
	//  地图
	private var mapX:Number=0;
	private var mapY:Number=0;
	private var mapS:Number=100;  //缩放大小
	private var mapMoveStep:Number=10;  //变动数
	private var mapBF:Number=4;  //缓冲级数
	//历史记录
	private var history:Array=new Array();
	private var historyL:Number=100;
	private var nowAt:Number=0;
	private var autoHistory:Boolean=true;   //自动记录历史
	private var playHId:Number=0;  //播放history进程id
	private var playing:Boolean=false;
	//  屏幕
	private var screen:Object=new Object();
	//  核心
	private var workingspeed:Number=0;//工作线程频率
	private var workingPId:Number=0;//工作线程id号
	private var _ifwork:Boolean=false; //是否已经开始工作
	private var _enable:Boolean=true;  //是否允许操作
	private var nowView:Object=new Object(); //当前的视觉状态
	private var willView:Object=new Object();  //需要变动的视觉状态
	
	//------------  路径
	private var me:Object=new Object;//类
	private var tag:Object=new Object;//实例
	private var map:Object=new Object;//地图
	//------------  版本及说明
	private var V1:String=""; //版权
	private var V2:String="";  //说明
	
	//---------------------------------------------  构造函数
	public function Viewer(tag1:Object,map1:Object,speed:Number){
		if(tag1==undefined)
		{
			say("未设置实例地址");
			tag1=null;
		}else{
			tag=tag1;
		}
		if(map1==undefined){
			say("未设置地图地址");
			map=null;
		}else{
			map=map1;
		}
		if(speed==undefined){
			say("设置默认工作速度25");
			workingspeed=25;
		}else{
			workingspeed=speed;
		}
		init();
		
	};
	//---------------------------------- 初始化函数
	private function init(){
		me=this;
		screen.left=0;
		screen.right=Stage.width;
		screen.top=0;
		screen.bottom=Stage.height;
		screen.x=Stage.width/2;
		screen.y=Stage.height/2;
		V1="\r**** Viewer 组件V1.2 *******\r";
		V1+="copyright by TOPHY\r";
		V1+="2005/1/8\r";
		V1+="QQ:40730122\r";
		V2+="\r------- 说明 ----------\r";
		V2+="使用说明：\r";
		V2+="\r";
		V2+="-- 属性部分\r";
		V2+="实例.ifwork=bool;   起动或关闭\r";
		V2+="实例.enable=bool;   允许或禁止地图操作\r";
		V2+="实例.findRStopAndBottom;  查找结果上下边缘空隙\r";
		V2+="实例.findRSleftAndRight;  查找结果左右边缘空隙\r";
		V2+="实例.mapStep=number; 设置地图滚动(缩放)步长\r";
		V2+="实例.mapBuf=number;  设置地图动作缓冲步长\r";
		V2+="实例.tagBuf=number;  设置控制器动作缓冲步长tagBuf\r";
		V2+="实例.workSpeed=number; 设置工作频率workSpeed\r";
		V2+="实例.historyLength=number;  设置历史记录长度\r";
		V2+="实例.autoSaveHistory=bool;  设置是否自动记录历史\r";
		V2+="\r";
		V2+="-- 函数部分\r";
		V2+="实例.setConMc(x,y,scale,visible); 设置控制器实例属性\r";
		V2+="实例.setCon(Object);   设置控制器实例地址\r";
		V2+="实例.setMap(Object);   设置地图地址setMap\r";
		V2+="实例.setScreen(left,right,top,bottom,x,y); 设置地图显示区域\r";
		V2+="实例.setMapXY(x,y,save:bool);  设置地图位置save为是否保存到历史记录\r";
		V2+="实例.clearHistory();   清除所有历史记录\r";
		V2+="实例.saveHistory(fg:bool);	记录历史,fg 是否覆盖当前历史记录之后的记录数据\r";
		V2+="实例.backHistory();    回滚到上一条历史记录\r";
		V2+="实例.nextHistory();    进入下一条历史记录\r";
		V2+="实例.playHistory(speed:Number,fx:bool); 播放历史记录,speed播放速度,fx播放方向true为正方向\r";
		V2+="实例.stopHistory();    停止播放历史记录\r";
		V2+="实例.saveView();		返回当前视觉状态\r";
		V2+="实例.setView(Object);  设置视觉状态\r";
		V2+="实例.fullScreen(save:bool);全屏地图save为是否保存到历史记录\r";
		V2+="实例.moveLR(fx:bool,x,save:bool);移动map x方向属性 fx为true时向左\r";
		V2+="实例.moveUD(fx:bool,y,save:bool);移动map y方向属性 fx为true时向上\r";
		V2+="实例.bigSmall(fx:bool,scale,save:bool); 放大或缩小map fx为true时放大\r";
		V2+="实例.setMapScale(scale,save:bool);   设置map大小\r";
		V2+="实例.setMapXY(x,y,save:bool);  设置map位置\r";
		V2+="实例.findTag(mcPath:Array);  在地图上查找mc。注：mcPath为map中mcname(相对map0级深)\r";
		V2+="\r";
		V2+="\r";
		V2+="-----------------------\r";
		V2+="\r";
		
		
		say(V1);
		if(showHelp)
		{
			say(V2);
		}
		say("完成初始化",true);
		//trace(Stage.width+"  "+Stage.height);
	};
	//起动工作线程
	private function startWorkingP(){
		workingPId=setInterval(workingP,workingspeed,me);
	};
	//停止工作线程
	private function stopWorkingP(){
		clearInterval(workingPId);
	};
	//====================================================工作线程
	private function workingP(me:Object){
		//-------------控制器部分
		me.mcOfConXY();
		me.mcOfConScale();
		me.mcOfConVisible();
		//----------------------
		
		if(me.map!=undefined || me.map!=null)
		{
			me.mapConXYwithBuf();
			me.mapScale();
		}else{
			//me.say("未设置地图");
			return;
		}
		if(!me.eqObject(me.nowView,me.willView))
		{
			var tempView:Object=new Object();
			tempView=me.saveView();
			if(me.eqObject(tempView,me.willView))
			{
				me.nowView=tempView;
			}else{
				me.setView(me.willView,true);
			}
		}
	};
	//===============================================================
	//----------- 输出函数
	private function say(st:Object,i:Boolean){
		if(!i){
			trace("○Viewer:"+st);
		}else
		{
			trace("●Viewer:"+st);
		}
	};
	//--------------------------------------------------------  内部功能函数
	
	//======= 控制器动作
	private function mcOfConXY(){
		if(tag==undefined && tag==null)
		{
			return ;
		}
		var xl:Number=myX-tag._x;
		var yl:Number=myY-tag._y;
		if(Math.abs(xl)>0.1 || Math.abs(yl)>0.1)
		{
			tag._x+=xl/myTx;
			tag._y+=yl/myTx;
		}
	};
	private function mcOfConScale(){
		if(tag==undefined && tag==null)
		{
			return ;
		}
		var sl:Number=myScale-tag._xscale;
		if(Math.abs(sl)>0.1)
		{
			tag._xscale+=sl/myTx;
			tag._yscale+=sl/myTx;
		}
	};
	private function mcOfConVisible(){
		if(tag==undefined && tag==null)
		{
			return ;
		}
		if(tag._visible!=myVisible)
		{
			tag._visible=myVisible;
		}
	};
	//=================
	///=================== map 动作
	//带缓冲map  x,y位置
	private function mapConXYwithBuf(){
		if(map!=undefined && map!=null)
		{
			var xl:Number=mapX-map._x;
			var yl:Number=mapY-map._y;
			if(Math.abs(xl)>0.1 || Math.abs(yl)>0.1)
			{
				map._x+=xl/mapBF;
				map._y+=yl/mapBF;
			}else{
				map._x=mapX;
				map._y=mapY;
			}
		}
	};
	//不带缓冲map   x,y位置
	private function mapConXYwithOutBuf(x:Number,y:Number){
		if(map!=undefined && map!=null)
		{
			var xl:Number=x-map._x;
			var yl:Number=y-map._y;
			if(Math.abs(xl)>0.1 || Math.abs(yl)>0.1)
			{
				map._x+=xl;
				map._y+=yl;
			}
		}
	};
	// 缩放部分
	private function mapScale(){
		if(map!=undefined && map!=null)
		{
			var sl:Number=mapS-map._xscale;
			if(Math.abs(sl)>0.1)
			{
				var sll:Number=sl/mapBF;
				var yuans:Number=map._xscale;
				map._xscale+=sll;
				map._yscale+=sll;
				var fx:Number=(screen.x-map._x)*(sll/yuans);
				var fy:Number=(screen.y-map._y)*(sll/yuans);
				mapConXYwithOutBuf((map._x-fx),(map._y-fy));
				mapX-=fx;
				mapY-=fy;
				//trace(screen.y);
			}else{
				
			}
		}
	};
	//两个object对比
	private function eqObject(obj1:Object,obj2:Object):Boolean
	{	
		for(var i in obj1)
		{
			if(typeof(obj1[i])!="object")
			{
				if(obj1[i]!=obj2[i])
				{
					return false;
				}
			}else{
				if(!eqObject(obj1[i],obj2[i]))
				{
					return false;
				}
			}
		}
		
		for(var i in obj2)
		{
			if(typeof(obj1[i])!="object")
			{
				if(obj1[i]!=obj2[i])
				{
					return false;
				}
			}else{
				if(!eqObject(obj1[i],obj2[i]))
				{
					return false;
				}
			}
		}
		
		return true;
	}
	//播放history的进程数
	private function playHistoryP(me:Object,fx:Boolean){
		if(fx){
			me.nextHistory();
			if(me.nowAt>=me.history.length)
			{
				me.stopHistory();
			}
		}else{
			me.backHistory();
			if(me.nowAt<0)
			{
				me.stopHistory();
			};
		}
	}
	//确定有否实例mc
	private function ifHaveMC(a:Array):Array{
		if(a.length>0)
		{
			var temp:Array=new Array();
			for(var i in a)
			{
				var s:String=a[i];
				
				if(map[s]!=undefined )
				{
					temp.push(a[i]);
				}
			}
			return temp;
		}else{
			return null;
		}
	};
	//-----------------------------------------------------  外部功能函数
	//查询定位
	public function findTag(a:Array,saveh:Boolean):Boolean{
		if(a.length>0)
		{
			var mc:Array=new Array();
			mc=ifHaveMC(a);
			say("要找mc= "+mc);
			if(mc.length>0)
			{
				var left:Number = Number.MAX_VALUE;
				var top:Number = Number.MAX_VALUE;
				var right:Number = Number.MIN_VALUE;
				var bottom:Number = Number.MIN_VALUE;
				for(var i in mc)
				{
					var mcs:MovieClip=map[mc[i]];
					if(left>mcs._x);
					{
						left=mcs._x;
					}
					if(top>mcs._y){
						top=mcs._y;
					}
					var w:Number=(mcs._x+mcs._width);
					var h:Number=(mcs._y+mcs._height);
					if(right<w)
					{
						right=w;
					}
					if(bottom<h)
					{
						bottom=h;
					}
					
				}
				left-=findRStopAndBottom;
				right+=findRStopAndBottom;
				top-=findRSleftAndRight;
				bottom+=findRSleftAndRight;
				var wid:Number=(right-left);
				var hei:Number=bottom-top;
				
				
				
		var mw:Number=screen.right-screen.left;
		var mh:Number=screen.bottom-screen.top;
			if((wid/hei)>(mw/mh))
			{
				//w
				var bl:Number=mw/wid;
				left*=int(bl*100)/100;
				top*=int(bl*100)/100;
				hei*=int(bl*100)/100;
				mapS=int(bl*100);
				map._xscale=mapS;
				map._yscale=mapS;
				mapX=screen.left-left;
				mapY=screen.top+(mh-hei)/2-top;
			}else{
				//h
				var bl:Number=mh/hei;
				left*=int(bl*100)/100;
				top*=int(bl*100)/100;
				wid*=int(bl*100)/100;
				mapS=int(bl*100);
				map._xscale=mapS;
				map._yscale=mapS;
				mapX=screen.left+(mw-wid)/2-left;
				mapY=screen.top-top;
			}
			if(autoHistory || saveh==true){
			saveHistory();
			}
		
				
				
				
				
				
			}else
			{
				say("没有找到要查找的mc");
				return false;
			}
		}else{
			say("无要查找的mc");
			return false;
		}
	};
	//播放历史记录
	public function playHistory(v:Number,fx){
		if(v==undefined && v==null && fx==undefined && fx==null)
		{
			say("播放命令参数不正确.",true);
			return;
		}
		if(fx)
		{
			nowAt=0;
		}else{
			nowAt=history.length;
		}
		playHId=setInterval(playHistoryP,v,me,fx);
		playing=true;
	};
	//停止播放历史记录
	public function stopHistory(){
		playing=false;
		clearInterval(playHId);
	};
	//返回所有历史记录
	public function History():Array{
		return history;
	};
	//清除所有历史记录
	public function clearHistory(){
		if(history.length>0)
		{
			history=new Array();
			nowAt=0;
		}
	};
	//记录历史
	public function saveHistory(fg:Boolean){
		if(!playing){
		if(fg){
			var l=history.length-1-nowAt;
			for(var i=0;i<l;i++)
			{
				history.pop();
			}
		}
		if(history.length<historyL)
		{
			nowAt=history.push(saveView());
		}else{
			history.shift();
			nowAt=history.push(saveView());
		}
		nowAt--;
		}
	};
	//回滚
	public function backHistory(){
		if(nowAt>=0){
			nowAt--;
			setView(history[nowAt]);
		}else
		{
			say("已经退回最早的历史记录了.");
		}
	};
	//前进
	public function nextHistory(){
		if(nowAt<history.length)
		{
			nowAt++;
			setView(history[nowAt]);
		}else
		{
			say("已经前进到目前历史记录了.")
		}
	};
	//保存当前视觉状态
	public function saveView(){
		var view:Object=new Object;
		var s:Object=new Object;
		s.left=screen.left;
		s.right=screen.right;
		s.top=screen.top;
		s.bottom=screen.bottom;
		s.x=screen.x;
		s.y=screen.y;
		view.screen=s;
		view.map=map;
		view.mapX=mapX;
		view.mapY=mapY;
		view.mapS=mapS;
		return view
	};
	//设置视觉状态
	public function setView(view:Object,noSave:Boolean){
		if(_enable){
		if(view!=undefined && view!=null)
		{
			screen.left=view.screen.left;
			screen.right=view.screen.right;
			screen.top=view.screen.top;
			screen.bottom=view.screen.bottom;
			screen.x=view.screen.x;
			screen.y=view.screen.y;
			map=view.map;
			mapX=view.mapX;
			mapY=view.mapY;
			mapS=view.mapS;
			
			willView=view;
			nowView=saveView;
		}
		}else{
			say("目前不允许操作.");
		}
	};
	//全屏地图
	public function fullScreen(saveh:Boolean){
		if(_enable){
		if(map!=undefined && map!=null){
		var mw:Number=screen.right-screen.left;
		var mh:Number=screen.bottom-screen.top;
		
			if((map._width/map._height)>(mw/mh))
			{
				//w
				var bl=mw*mapS/map._width;
				bl=int(bl);
				mapS=bl;
				map._xscale=bl;
				map._yscale=bl;
				mapY=screen.top+(mh-(map._height))/2;
				mapX=screen.left;
			}else{
				//h
				var bl=mh*mapS/map._height;
				bl=int(bl);
				mapS=bl;
				map._xscale=bl;
				map._yscale=bl;
				mapY=screen.top;
				mapX=screen.left+(mw-map._width)/2;
			}
			if(autoHistory || saveh==true){
			saveHistory();
			}
		}
		}else{
			say("目前不允许操作.");
		}
	};
	//变动map x,y,scal属性moveLR
	public function moveLR(fx:Boolean,x:Number,saveh:Boolean){
		if(_enable){
		if(x!=null && x!=undefined)
		{
			mapX+=x;
		}else{
			if(fx){
				//向左
				mapX-=mapMoveStep;
			}else
			{
				//向右
				mapX+=mapMoveStep;
			}
		}
		if(autoHistory || saveh==true){
			saveHistory();
		}
		}else{
			say("目前不允许操作.");
		}
	};
	public function moveUD(fx:Boolean,y:Number,saveh:Boolean){
		if(_enable){
		if(y!=null && y!=undefined)
		{
			mapY+=y;
		}else{
			if(fx){
				//向上
				mapY-=mapMoveStep;
			}else{
				//向下
				mapY+=mapMoveStep;
			}
		}
		if(autoHistory || saveh==true){
			saveHistory();
		}
		}else{
			say("目前不允许操作.");
		}
	};
	public function bigSmall(fx:Boolean,s:Number,saveh:Boolean)
	{
		if(_enable){
		if(s!=null && s!=undefined)
		{
			mapS+=s;
		}else
		{
			if(fx){
				//放大
				mapS+=mapMoveStep;
			}else{
				//缩小
				mapS-=mapMoveStep;
			}
		}
		if(autoHistory || saveh==true){
			saveHistory();
		}
		}else{
			say("目前不允许操作.");
		}
	};
	
	//设置map scale属性
	public function setMapScale(s:Number,saveh:Boolean)
	{
		if(_enable){
		if(s!=null && s!=undefined)
		{
			mapS=s;
			if(autoHistory || saveh==true){
			saveHistory();
			}
		}
		}else{
			say("目前不允许操作.");
		}
	};
	//设置map x,y属性
	public function setMapXY(x:Number,y:Number,saveh:Boolean){
		if(_enable){
		var b:Boolean=false;
		if(x!=null && x!=undefined)
		{
			mapX=x;
			b=true;
		}
		if(y!=undefined && y!=null)
		{
			mapY=y;
			b=true;
		}
		if(b)
		{
			if(autoHistory || saveh==true){
			saveHistory();
			}
		}
		}else{
			say("目前不允许操作.");
		}
	};
	//设置地图显示区域setScreen
	public function setScreen(left,right,top,bottom,x,y)
	{
		if(left!=null && left!=undefined)
		{
			screen.left=left;
		}
		if(right!=null && right!=undefined)
		{
			screen.right=right;
		}
		if(top!=null && top!=undefined)
		{
			screen.top=top;
		}
		if(bottom!=null && bottom!=undefined)
		{
			screen.bottom=bottom;
		}
		if(x!=null && x!=undefined)
		{
			screen.x=x;
		}
		if(y!=null && y!=undefined)
		{
			screen.y=y;
		}
	}
	//设置地图地址
	public function setMap(map1:Object)
	{
		if(_enable){
		if(map1==undefined)
		{
			return;
		}else
		{
			map=map1;
		}
		}else{
			say("目前不允许操作.");
		}
	};
	//设置控制器实例地址
	public function setCon(tag1:Object)
	{
		if(tag1==undefined)
		{
			return;
		}else{
			tag=tag1;
		}
	};
	//设置控制器
	public function setConMc(x:Number,y:Number,s:Number,v:Boolean)
	{
		if(x!=null && x!=undefined)
		{
			myX=x;
		}
		if(y!=null && y!=undefined)
		{
			myY=y;
		}
		if(s!=null && s!=undefined)
		{
			myScale=s;
		}
		if(v!=null && v!=undefined)
		{
			myVisible=v;
		}
	}
	//------------------------------------------------------  get set 函数
	//允许操作开关
	public function set enable(b:Boolean){
		if(b!=_enable)
		{
			if(b){
				say("设置操作允许.");
			}else{
				say("设置操作不允许");
			}
			_enable=b;
		}
	};
	//设置是否自动记录历史
	public function set autoSaveHistory(b:Boolean){
		autoHistory=b;
	};
	public function get autoSaveHistory(){
		return autoHistory;
	};
	//设置历史记录长度
	public function set historyLength(l:Number){
		if(history.length<=l)
		{
			historyL=l
		}else
		{
			say("已存历史记录被截！",true);
			var ll:Number=history.length-l;
			for(var i=0;i<ll;i++)
			{
				history.shift();
			}
		}
	};
	public function get historyLength():Number{
		return historyL;
	};
	//设置工作频率
	public function set workSpeed(speed:Number){
		if(!_ifwork && speed!=null && speed!=undefined)
		{
			workingspeed=speed;
		}else{
			say("工作速度设置必须在开始工作之前设置.",true);
		}
	};
	public function get workSpeed():Number{
		return workSpeed;
	};
	//设置控制器动作缓冲步长
	public function set tagBuf(buf:Number){
		if(buf!=null && buf!=undefined)
		{
			myTx=buf;
		}
	}
	public function get tagBuf():Number{
		return myTx;
	}
	//设置地图动作缓冲步长
	public function set mapBuf(buf:Number){
		if(buf!=null && buf!=undefined)
		{
			mapBF=buf;
		}
	}
	public function get mapBuf():Number{
		return mapBF;
	};
	//设置地图变动步长
	public function set mapStep(s:Number){
		if(s!=undefined && s!=null)
		{
			mapMoveStep=s;
		}
	}
	public function get mapStep():Number{
		return mapMoveStep;
	};
	// 起动及停止
	public function set ifwork(b:Boolean){
		if(b)
		{
			if(!_ifwork)
			{
				startWorkingP();
				say("起动Viewer.",true);
				_ifwork=true;
			}else{
				say("已经起动");
			}
		}else
		{
			if(_ifwork)
			{
				stopWorkingP();
				say("停止Viewer.",true);
				_ifwork=false;
			}else{
				say("已经停止");
			}
		}
	};
	public function get ifwork():Boolean{
		return _ifwork;
	};
}